package eclipse.lib.graphics;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.media.opengl.*;
import com.sun.opengl.util.*;
/**
 *The GLInterface links the OpenGL object to the Application. It also handles keyboard events
 */
public class GLInterface implements KeyListener
{
	private static final long serialVersionUID = 1L;
	private GLCanvas glCanvas;
	private Animator glAnimat;
	private boolean isLeft		= false;
	private boolean isRight		= false;
	private boolean isUp		= false;
	private boolean isDown		= false;
	private boolean isTab		= false;
	/**
	 * Creates the GLInterface with a specified surface and a OpenGL object
	 * @param dstSurface Surface to overwrite for OpenGL
	 * @param glObject OpenGL object
	 */
	public GLInterface(Container dstSurface, GLEventListener glObject)
	{
		glCanvas = new GLCanvas();		
		glAnimat = new Animator();
		
		glCanvas.addGLEventListener(glObject);
		glCanvas.addKeyListener(this);
		glAnimat = new Animator(glCanvas);
		dstSurface.add(glCanvas);
		dstSurface.validate();
		glAnimat.start();
	}
	public void keyPressed(KeyEvent e) 
	{
		if(e.getKeyCode() == KeyEvent.VK_TAB)
		{
			isTab = true;			
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP)
		{
			if(isDown)
				isDown		= false;
				isUp		= true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_DOWN)
		{
			if(isUp)
				isUp		= false;
				isDown		= true;

		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			if(isRight)			
				isRight	= false;
			isLeft			= true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			if(isLeft)
				isLeft		= false;
			isRight		= true;
		}			
	}

	public void keyReleased(KeyEvent e) 
	{
		if(e.getKeyCode() == KeyEvent.VK_UP)
			isUp			= false;
		if(e.getKeyCode() == KeyEvent.VK_DOWN)
			isDown			= false;
		if(e.getKeyCode() == KeyEvent.VK_LEFT)
			isLeft			= false;
		if(e.getKeyCode() == KeyEvent.VK_RIGHT)
			isRight			= false;		
	}

	public void keyTyped(KeyEvent e) 
	{
		
	}		
	/** Returns if right was pressed */
	public boolean getRight()
	{
		return isRight;
	}
	/** Returns if left was pressed */
	public boolean getLeft()
	{
		return isLeft;
	}
	/** Returns if up was pressed */
	public boolean getUp()
	{
		return isUp;
	}
	/** Returns if down was pressed */
	public boolean getDown()
	{
		return isDown;
	}
	/** Returns if tab was pressed */
	public boolean getTab()
	{
		return isTab;
	}
}